﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.Xpo;

namespace GDIT.Model
{
    [NonPersistent]
    [MemberDesignTimeVisibility(false)]
    public class PersistentClass : XPBaseObject
    {
        private int m_Oid = -1;

        /// <summary>
        /// OID:对象标识，即通常表中的自动增长主键ID
        /// </summary>
        [Key(true)]
        [Persistent("OID")]
        [DbType("int")]
        public int OID
        {
            get { return this.m_Oid; }
            set
            {
                this.m_Oid = value;
                OnChanged("Oid");
            }
        }

        private int _Index = 999999;
        /// <summary>
        /// 自定义的排序，没用的话可以删除忽略
        /// </summary>
        public int Index
        {
            get
            {
                return this._Index;
            }
            set
            {
                if (this._Index != value)
                {
                    this._Index = value;
                    if (!IsLoading)
                    {
                        this.Modified = true;
                    }
                }
            }
        }

        private Boolean _Modified = false;
        /// <summary>
        /// 修改标记，为子类预留的接口
        /// </summary>
        [NonPersistent]
        public Boolean Modified
        {
            get
            {
                return this._Modified;
            }
            set
            {
                if (!IsLoading && this._Modified != value)
                {
                    this._Modified = value;
                }
            }
        }

        /// <summary>
        /// 数据检验，可通过 override 重写次方法，在保存对象前调用以进行数据检验
        /// </summary>
        /// <returns>返回 字符串类型：数据错误的提醒信息</returns>
        public virtual string GetDataError()
        {
            return string.Empty;
        }

        /// <summary>
        /// 生成一个新实体并复制数据到新实体，需要重写（用不上的话可以删掉）
        /// </summary>
        /// <param name="CopyAggregated">是否复制聚集属性</param>
        /// <returns>返回新的已复制数据的业务对象</returns>
        public virtual object Clone(Boolean CopyAggregated = true)
        {
            //return BO.ObjectHelper.Clone(Me, CopyAggregated);
            return null;
        }

        /// <summary>
        /// 该实体是否在数据库存在
        /// </summary>
        /// <returns></returns>
        public virtual Boolean DBExist()
        {
            //return BO.ObjectHelper.DBExist(Me);
            return false;
        }

        /// <summary>
        /// 检查业务对象（数据表）某字段的唯一性
        /// </summary>
        /// <param name="FieldName">字段名</param>
        /// <param name="Value">字段值</param>
        /// <returns>若是无此数据表对象返回False，否则返回True</returns>
        public virtual Boolean ExclusiveString(string FieldName, string Value)
        {
            return ObjectHelper.ExclusiveString(this, FieldName, Value);
            //return false;
        }

        /// <summary>
        /// 根据自定义SQL中的WHERE语句检查业务对象（数据表）的唯一性
        /// </summary>
        /// <param name="Where">SQL 的 Where 语句</param>
        /// <returns>若是唯一或无此数据表返回True，否则返回False</returns>
        public virtual Boolean ExclusiveByWhere(string Where)
        {
            return ObjectHelper.ExclusiveByWhere(this.ClassInfo, Where, this.Session);
            //return false;
        }

        /// <summary>
        /// 根据属性名获得该属性值
        /// </summary>
        /// <param name="Name"></param>
        /// <returns></returns>
        object GetValueByMemberName(string Name)
        {
            try
            {
                return this.ClassInfo.GetMember(Name).GetValue(this);
            }
            catch (Exception)
            {
                return null;
            }
        }

        /// <summary>
        /// 根据属性名设置该属性值
        /// </summary>
        /// <param name="Name">属性名</param>
        /// <param name="Value">属性值</param>
        void SetValueByMemberName(string Name, object Value)
        {
            this.ClassInfo.GetMember(Name).SetValue(this, Value);
        }

        /// <summary>
        /// 新增对象时将对象设定为未被修改
        /// </summary>
        public PersistentClass()
            : base()
        {
            this._Modified = false;
        }

        /// <summary>
        /// 新增对象时将对象设定为未被修改，同时定义对象使用的数据库SESSION
        /// </summary>
        /// <param name="DBsession">数据库SESSION</param>
        public PersistentClass(Session DBsession)
            : base(DBsession)
        {
            this._Modified = false;
        }

        /// <summary>
        /// 保存对象
        /// </summary>
        new public virtual void Save()
        {
            base.Save();
            this._Modified = false;
        }

        /// <summary>
        /// 删除对象
        /// </summary>
        new public virtual void Delete()
        {
            base.Delete();
            this._Modified = false;
        }

        /// <summary>
        /// 刷新对象
        /// </summary>
        new public virtual void Reload()
        {
            base.Reload();
            this._Modified = false;
        }

        /// <summary>
        /// 加载之后的操作，预留接口，子类重写
        /// </summary>
        new protected virtual void AfterLoad()
        {
            this._Modified = false;
            base.Reload();
        }
    }
}
